package chujisuanfa;

import java.util.*;

public class FirstUniqChar {

    public static int firstUniqChar(String s){
        Map<Character,Integer> map = new HashMap<>();
        Deque<Character> queue = new ArrayDeque<>();
        int target=0;
        for(int i=0;i<s.length();++i){
            char c = s.charAt(i);
            queue.add(c);
            if(map.containsKey(c)){
                map.replace(c,map.get(c)+1);

            }else{
                map.put(c,1);
            }
            while(!queue.isEmpty()&&map.get(queue.getFirst())!=1){
                target++;
                queue.removeFirst();

            }
        }
        return target==s.length()?-1:target;
    }

    public static int myAtoi(String s) {
        int c=0;
        while(s.charAt(c)==' ') c++;

        int target = 0;
        int t = s.charAt(c);

        if(t==43) c++;
        if(t==45) {
            c++;
            t=s.charAt(c)-'0';
            while(t>=0&&t<=9){
                target+=t;
                c++;
                t=s.charAt(c)-'0';
                if(target<=Integer.MAX_VALUE/10) return Integer.MIN_VALUE;
                target*=10;

                target+=t;
            }
            return -target;
        }
        t=s.charAt(c)-'0';
        while(t>=0&&t<=9){
            if(target>=Integer.MAX_VALUE/10) return Integer.MAX_VALUE;
            target*=10;
            while (s.charAt(c++)==' ');
            target+=t;
            c++;
            t=s.charAt(c)-'0';
            target+=t;
        }
        return target;
    }

    public static void main(String[] args) {

        System.out.println(myAtoi("+12w12e"));
    }
}
